package com.berchen.数据结构.单链表;

public class SignleLinketListTest {
    public static void main(String[] args) {
        SignleLinketList linketList = new SignleLinketList();
        linketList.add(new Node(2,"chen","陈"));
        linketList.add(new Node(1,"chen1","陈1"));
        linketList.add(new Node(5,"chen3","陈3"));
        linketList.add(new Node(10,"chen5","陈5"));
        linketList.add(new Node(4,"chen4","陈4"));

        linketList.list();


        SignleLinketList linketList1 = new SignleLinketList();
        linketList1.add(new Node(20,"chen","陈"));
        linketList1.add(new Node(1,"chen1","陈1"));
        linketList1.add(new Node(3,"chen3","陈3"));
        linketList1.add(new Node(5,"chen5","陈5"));
        linketList1.add(new Node(4,"chen4","陈4"));

        SignleLinketListTest test = new SignleLinketListTest();
        Node node = test.heBIn(linketList.getHead(), linketList1.getHead());

        test.list(node);

    }
    public void list(Node node){
        Node temp=node;
        while (temp!=null){
            System.out.println(temp.toString());
            temp=temp.next;
        }
    }

    /**
     * 合并两个链表
     * @param linketList1 链表1的头节点
     * @param linketList2 链表2的头节点
     * @return 返回的就是一个新的链表
     */
    public Node heBIn(Node linketList1,Node linketList2){

        Node headNew=new Node(0,"","");
        Node temp=headNew;
        while (true){
            if(linketList1==null&&linketList2==null){
                break;
            }
            if(linketList1==null&&linketList2!=null){
                temp.next=linketList2;
                temp=temp.next;
                linketList2=linketList2.next;
            }
            if(linketList2==null&&linketList1!=null){
                temp.next=linketList1;
                temp=temp.next;
                linketList1=linketList1.next;
            }
            if(linketList1!=null&&linketList2!=null) {
                if (linketList1.getNo() <= linketList2.getNo()) {
                    temp.next = linketList1;
                    temp = temp.next;
                    linketList1 = linketList1.next;
                } else {
                    temp.next = linketList2;
                    temp = temp.next;
                    linketList2 = linketList2.next;
                }
            }
        }
        return headNew.next;

    }
}
